package cn.wolfcode.rbac.service.impl;

import cn.wolfcode.rbac.domain.Customer;
import cn.wolfcode.rbac.mapper.CustomerMapper;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.ICustomerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CustomerServiceImpl implements ICustomerService {
    @Autowired
    private CustomerMapper mapper;

    @Override
    @Transactional(readOnly = true)
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Customer> list = mapper.selectCustomer(qo);
        for(Customer customer : list){
            if(customer.getGender()==0){
                customer.setDisplayGender("男");
            }else{
                customer.setDisplayGender("女");
            }
            switch (customer.getStatus()){
                case 0:
                    customer.setDisplayStatus("潜在客户");
                    continue;
                case 1:
                    customer.setDisplayStatus("正式客户");
                    continue;
                case 2:
                    customer.setDisplayStatus("资源池客户");
                    continue;
                case 3:
                    customer.setDisplayStatus("开发失败客户");
                    continue;
                case 4:
                    customer.setDisplayStatus("流失客户");
                    continue;
                default:
                    customer.setDisplayStatus("移交历史");
                    continue;
            }
        }
        return new PageInfo(list);
    }

    @Override
    public void saveOrUpdate(Customer customer) {
        if(customer.getId()!=null){
            mapper.update(customer);
        }else {
            mapper.insert(customer);
        }
    }

    @Override
    public void updateStatus(Customer customer) {
        this.mapper.updateStatus(customer);
    }

    @Override
    public void updateSeller(Customer customer) {
        this.mapper.updateSeller(customer);
    }
}
